#define _CRT_SECURE_NO_WARNINGS
class Solution {
public:
    string multiply(string num1, string num2) {
        if (num1 == "0" || num2 == "0")
        {
            return "0";
        }
        string ret = "0";
        int end1 = num1.size() - 1;
        int end2 = num2.size() - 1;
        for (int i = end2; i >= 0; i--)
        {
            string cur;
            int add = 0;
            for (int j = end2; j > i; j--)
            {
                cur.push_back(0);
            }
            int x = num2[i] - '0';
            for (int j = end1; j >= 0; j--)
            {
                int y = num1[j] - '0';
                int pro = y * x + add;
                cur.push_back(pro % 10);
                add = pro / 10;
            }
            while (add != 0)
            {
                cur.push_back(add % 10);
                add /= 10;
            }
            reverse(cur.begin(), cur.end());
            for (auto& ch : cur)
            {
                ch += '0';
            }
            ret = addStrings(ret, cur);

        }
        return ret;
    }


    string addStrings(string num1, string num2) {
        int end1 = num1.size() - 1;
        int end2 = num2.size() - 1;

        int next = 0;
        string ret;
        while (end1 >= 0 || end2 >= 0)
        {

            int val1 = end1 >= 0 ? num1[end1--] - '0' : 0;
            int val2 = end2 >= 0 ? num2[end2--] - '0' : 0;
            int val = val1 + val2 + next;
            next = val / 10;
            val = val % 10;
            ret.push_back(val + '0');

        }
        if (next == 1)
        {
            ret.push_back('1');
        }
        reverse(ret.begin(), ret.end());
        return ret;
    }
};